package com.ruoyi.cms.controller;

import com.ruoyi.cms.domain.ArticleDraft;
import com.ruoyi.cms.service.IArticleDraftService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文章草稿Controller
 *
 * @author ruoyi
 * @date 2020-12-06
 */
@Controller
@RequestMapping("/cms/draft")
public class ArticleDraftController extends BaseController {
    private String prefix = "cms/draft";

    @Autowired
    private IArticleDraftService articleDraftService;

    @RequiresPermissions("cms:draft:view")
    @GetMapping()
    public String draft() {
        return prefix + "/list";
    }

    /**
     * 查询文章草稿列表
     */
    @RequiresPermissions("cms:draft:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ArticleDraft articleDraft) {
        startPage();
        List<ArticleDraft> list = articleDraftService.selectArticleDraftList(articleDraft);
        return getDataTable(list);
    }

    /**
     * 导出文章草稿列表
     */
    @RequiresPermissions("cms:draft:export")
    @Log(title = "文章草稿", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ArticleDraft articleDraft) {
        List<ArticleDraft> list = articleDraftService.selectArticleDraftList(articleDraft);
        ExcelUtil<ArticleDraft> util = new ExcelUtil<ArticleDraft>(ArticleDraft.class);
        return util.exportExcel(list, "draft");
    }

    /**
     * 新增文章草稿
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存文章草稿
     */
    @RequiresPermissions("cms:draft:add")
    @Log(title = "文章草稿", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ArticleDraft articleDraft) {
        return toAjax(articleDraftService.insertArticleDraft(articleDraft));
    }

    /**
     * 修改文章草稿
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        ArticleDraft articleDraft = articleDraftService.selectArticleDraftById(id);
        mmap.put("articleDraft", articleDraft);
        return prefix + "/edit";
    }

    /**
     * 修改保存文章草稿
     */
    @RequiresPermissions("cms:draft:edit")
    @Log(title = "文章草稿", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ArticleDraft articleDraft) {
        return toAjax(articleDraftService.updateArticleDraft(articleDraft));
    }

    /**
     * 删除文章草稿
     */
    @RequiresPermissions("cms:draft:remove")
    @Log(title = "文章草稿", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(articleDraftService.deleteArticleDraftByIds(ids));
    }
}
